package com.example.pcl.controller.user;


import com.example.pcl.constant.MessageConstants;
import com.example.pcl.pojo.dto.*;
import com.example.pcl.result.Result;
import com.example.pcl.service.IUsersService;
import com.example.pcl.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表，存储所有用户信息 前端控制器
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@RestController("userUsersController")
@RequestMapping("/user/user")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "用户端用户相关接口")
public class UsersController {

    private final IUsersService userService;


    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<?> register(@RequestBody RegisterDto registerDto) {
        log.info("用户尝试注册: {}", registerDto);
        userService.register(registerDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<?> login(@RequestBody LoginDto loginDto) {
        log.info("用户尝试登录: {}", loginDto);
        String token = userService.login(loginDto);
        return Result.success(token, MessageConstants.LOGIN_SUCCESS);
    }


    @GetMapping("/info")
    @Operation(summary = "获取用户个人信息")
    public Result<?> getUserInfo() {
        Integer userId = ThreadLocalUtil.getId();
        log.info("用户获取个人信息, 用户ID: {}", userId);
        return Result.success(userService.finById(userId), MessageConstants.SELECT_OK);
    }

    @PutMapping("/update")
    @Operation(summary = "用户更新个人信息")
    public Result<?> updateUserInfo(@RequestBody UserDto updateUserDto) {
        log.info("用户更新个人信息,数据: {}", updateUserDto);
        userService.updateUserInfo(updateUserDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @PostMapping("/changePassword")
    @Operation(summary = "用户修改密码")
    public Result<?> changePassword(@RequestBody ChangePasswordDto changePasswordDto) {
        log.info("用户尝试修改密码:{}", changePasswordDto);
        userService.changePassword(changePasswordDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
